package com.ourteam.product.api;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.product.ui.dao.ProductUIActionDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIActionBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/UI/productUIAction")
public class ProductUIActionAPI extends
		AbstractCRUDServiceController<ProductUIActionDAOQueryBean, ProductUIActionSearchModel, ProductUIActionBean> {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@Override
	protected ProductUIActionDAOQueryBean newQueryBean() {
		return new ProductUIActionDAOQueryBean();
	}

	@Override
	protected int queryCount(ProductUIActionDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIActionCount(queryBean);
	}

	@Override
	protected ProductUIActionBean[] queryData(ProductUIActionDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIActions(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {
		PRODUCT_UI_SERVICE.deleteProductUIActions(ids);
	}

	@Override
	protected ProductUIActionBean getDataById(long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUIActionById(id);
	}

	@Override
	protected void saveData(ProductUIActionBean dataBean) throws Exception {
		dataBean.setLastModifyTime(new Date());
		dataBean.setLastModifyUser(getLoginUserName());

		if (dataBean.getParentId() != 0 && dataBean.getSortIndex() == 0) {

			ProductUIActionDAOQueryBean queryBean = new ProductUIActionDAOQueryBean();

			queryBean.createCriteria().andDomainIdEqualTo(dataBean.getDomainId())
					.andParentIdEqualTo(dataBean.getParentId());

			dataBean.setSortIndex(PRODUCT_UI_SERVICE.queryProductUIActionCount(queryBean) + 1);
		}

		if (dataBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUIAction(dataBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUIAction(dataBean);
		}
	}

}
